package pacman.entries.pacman.ml.features;

import java.util.LinkedList;

import pacman.game.Game;
import pacman.game.Constants.GHOST;

// temporal feature
// returns value between 0 and 1
// returns 0 if ghost has circled back to same location
// returns 1 if ghosts has moved farthest distance possible from original location
public class GhostMovementFeature extends FeatureExtractor {
	private GHOST target;
	public static final int time = 125;   // time span to consider feature over
	private LinkedList<Integer> locations;
	public GhostMovementFeature(float ghost) {
		target = GHOST.values()[(int)ghost];
		locations = new LinkedList<Integer>();
	}
	public float Extract(Game game) {
		int ghostCurrentNode = game.getGhostCurrentNodeIndex(target);
		int ghostFirstNode = 0;
		
		locations.addFirst(ghostCurrentNode);
		if(locations.size() < time) {
			ghostFirstNode = locations.getLast();
		}
		else {
			ghostFirstNode = locations.removeLast();
		}
		
		int dist = 0;
		try {
			dist = game.getShortestPathDistance(ghostFirstNode,ghostCurrentNode);
		}
		catch (Exception e){
		}
		
		return Math.min(((float)(dist))/time, 1.0f);
	}
}